package com.gyms.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.gyms.constant.Constants;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import constant.EventConstant;
import weight.UiEvent;


/**
 * 微信支付的回调
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constants.App_Id);
        api.handleIntent(getIntent(), this);
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {

        if(resp.getType()== ConstantsAPI.COMMAND_PAY_BY_WX){
            if(resp.errCode==0){//支付成功
                EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_WX_PAY_SUCCESS,null));
            }else if(resp.errCode==-1){//支付失败
                EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_WX_PAY_FAIL,null));
            }else if(resp.errCode==-2){//用户取消
                EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_WX_PAY_FAIL,null));
            }
            finish();
        }
    }
}
